package com.fingard.dsp.bank.directbank.spdb01.authforrec;

/**
 * String帮助
 *
 * @author T-chenqy
 */
public final class StringUtils {

    public static boolean isEmpty(Object obj) {
        if (obj == null || "".equals(obj.toString().trim())) {
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * convert Object to String
     *
     * @param obj
     * @return
     */
    public static String objectToString(Object obj) {
        return obj == null ? null : obj.toString();
    }

    /**
     * 把byte数组转换成16进制字符串（字母大写）
     *
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        StringBuffer stringBuilder = new StringBuffer("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String toHexString(String str) {
        return bytesToHexString(str.getBytes());
    }

    /**
     * 把16进制字符串（字母大写）转换成byte数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}
